package com.shanshan.crowd.mvc.controller;

import com.shanshan.crowd.entity.Role;
import com.shanshan.crowd.service.api.AdminService;
import com.shanshan.crowd.service.api.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @Description
 * @ClassName AssignController
 * @Author 山山大魔王
 * @Date 2022/7/25 15:46
 * @Version 1.0
 */
@Controller
public class AssignController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private AdminService adminService;

    @RequestMapping("/assign/do/role/assign.html")
    public String saveRelationship(
            @RequestParam("adminId") Integer adminId,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("keyword") String keyword,

            @RequestParam(value = "roleIdList", required = false ) List<Integer> roleIdList
    ){
        adminService.saveRelationship(adminId, roleIdList);
        return "redirect:/admin/page.html?pageNum="+pageNum+"&keyword="+keyword;
    }


    @RequestMapping("/assign/to/assign/role/page.html")
    public String toAssignRolePage(
            @RequestParam("adminId") Integer adminId,
            ModelMap modelMap
    ){
        // 1.查询已分配的角色
        List<Role> assignedRoleList = roleService.getAssignedRole(adminId);
        // 2.查询未分配的角色
        List<Role> unAssignedRoleList = roleService.getUnAssignedRole(adminId);
        // 3.存入模型
        modelMap.addAttribute("assignedRoleList",assignedRoleList);
        modelMap.addAttribute("unAssignedRoleList",unAssignedRoleList);

        return "assign-role";
    }
}
